package com.inool.controller;


import com.inool.service.SysMenuService;
import com.inool.vo.AssignMenuVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inool.common.result.Result;
import com.inool.model.BaseEntity;
import com.inool.model.Student;
import com.inool.model.SysMenu;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 5unkzh1
 * @since 2024-02-04
 */
@Tag(name="菜单管理接口")
@RestController
@RequestMapping("/api/sys-menu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @Operation(summary  = "根据角色获取菜单")
    @GetMapping("toAssign/{roleCode}")
    public Result<List<SysMenu>> toAssign(@PathVariable String roleCode) {
        List<SysMenu> list = sysMenuService.findSysMenuByRoleCode(roleCode);
        return Result.ok(list);
    }

    @Operation(summary  = "获取树状菜单")
    @GetMapping("all")
    public Result<List<SysMenu>> all() {
        List<SysMenu> list = sysMenuService.findSysMenu();
        return Result.ok(list);
    }

    @Operation(summary  = "分页查询菜单")
    @GetMapping("page")
    public Result<IPage<SysMenu>> pageSysMenu(@RequestParam long current, @RequestParam long size) {
        IPage<SysMenu> page = new Page<>(current, size);
        IPage<SysMenu> list = sysMenuService.pageSysMenu(page);
        return Result.ok(list);
    }

    @Operation(summary = "给角色分配权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssignMenuVo assignMenuVo) {
        sysMenuService.doAssign(assignMenuVo);
        return Result.ok();
    }


    @Operation(summary = "获取菜单")
    @GetMapping("findNodes")
    public Result<List<SysMenu> > findNodes() {
        List<SysMenu> list = sysMenuService.findNodes();
        return Result.ok(list);
    }

    @Operation(summary = "新增菜单")
    @PostMapping("save")
    public Result<Void> save(@RequestBody SysMenu permission) {
    
        sysMenuService.save(permission);
        return Result.ok();
    }

    @Operation(summary = "修改菜单")
    @PutMapping("update")
    public Result updateById(@RequestBody SysMenu permission) {
        sysMenuService.updateById(permission);
        return Result.ok();
    }

    @Operation(summary = "删除菜单")
    @PostMapping("remove/{id}")
    public Result<Boolean> remove(@PathVariable String id) {        
        return Result.ok(sysMenuService.removeMenuById(id));
    }
}


